package io.renren.common.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DfsUtil {
    /**
     *
     * @param multipartFile spring多媒体文件封装对象
     * @param url 文件上传服务器地址
     * @return
     */
    public static FileServerResponse uploadFileByMultipartFile(MultipartFile multipartFile, String url) throws IOException {
            String contentType_str = multipartFile.getContentType();
            ContentType contentType = ContentType.create(contentType_str);
            return uploadFile(multipartFile.getOriginalFilename(), multipartFile.getBytes(), contentType, url);
    }

    /**
     *
     * @param fileName
     * @param contents
     * @param contentType
     * @return
     */
    public static FileServerResponse uploadFileByBinary(String fileName, byte[] contents, ContentType contentType, String url){
        return uploadFile(fileName, contents, contentType, url);
    }

    private static FileServerResponse uploadFile(String fileName,byte[] contents, ContentType contentType, String url){
        FileServerResponse fileServerResponse = new FileServerResponse(false);
        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
        httpPost.setConfig(requestConfig);
        try {
            HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("file", contents, contentType, fileName).build();
            httpPost.setEntity(entity);

            CloseableHttpResponse response = client.execute(httpPost);
            try {
                System.out.println("文件上传结果=======：" + response.getStatusLine());
                if(200 != response.getStatusLine().getStatusCode()){
                    return fileServerResponse;
                }
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    String uploadresult = EntityUtils.toString(response.getEntity());
                    System.out.println(uploadresult);
                    fileServerResponse = JSON.parseObject(uploadresult, FileServerResponse.class);
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileServerResponse;
    }

    public static void main(String[] args) {
//        File file = new File("E:\\xiaoxiong.jpg");
        File file = new File("E:\\菏易行\\W2、平台各服务器\\公司服务器信息.txt");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
//            MultipartFile multipartFile = new MockMultipartFile("file",file.getName(),ContentType.IMAGE_JPEG.getMimeType(),fileInputStream);
            MultipartFile multipartFile = new MockMultipartFile("file",file.getName(),ContentType.DEFAULT_TEXT.getMimeType(),fileInputStream);
            uploadFileByMultipartFile(multipartFile,"http://file.ehualu.com:8083/fastdfs/upload/file/sample");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
